-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/python asyncio #826
Fix/python asyncio #826
Conversation
Apart from the code comments, could you please detail in the commit messages what this does, and especially why we want it? I don't have context about this change. However, the code changes look something reasonable; thanks for the work! |
its the fix for python 3.10-3.11. alongside latest python patch releases were made breaking change for lower level asyncio api. Also seems like using asyncio api |
c545a4c
to
3cb131b
Compare
} handlers[] = { | ||
{ "create_task", &ctx_data->loop_create_task }, | ||
{ "add_reader", &ctx_data->loop_add_reader }, | ||
{ "remove_reader", &ctx_data->loop_remove_reader }, | ||
{ "call_soon", &ctx_data->loop_call_soon }, | ||
{ "run_until_complete", &ctx_data->loop_run_until_complete }, | ||
{ "create_future", &ctx_data->loop_create_future }, | ||
}; | ||
|
||
loop = NULL; | ||
|
||
asyncio = PyImport_ImportModule("asyncio"); | ||
if (nxt_slow_path(asyncio == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to import module 'asyncio'"); | ||
nxt_python_print_exception(); | ||
goto fail; | ||
} | ||
|
||
event_loop_func = "get_loop"; | ||
runner_class = "Runner"; | ||
runner_ref = PyDict_GetItemString(PyModule_GetDict(asyncio), | ||
runner_class); | ||
if (nxt_slow_path(runner_ref == NULL)) { | ||
nxt_unit_alert(NULL, | ||
"Python failed to get '%s' from module 'asyncio'", | ||
runner_class); | ||
goto fail; | ||
} | ||
|
||
runner = PyObject_CallObject(runner_ref, NULL); | ||
if (nxt_slow_path(runner == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to call 'asyncio.%s'", | ||
runner_class); | ||
goto fail; | ||
} | ||
ctx_data->runner = runner; | ||
loop_ref = PyObject_GetAttrString(runner, event_loop_func); | ||
loop = PyObject_CallObject(loop_ref, NULL); | ||
if (nxt_slow_path(runner == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to call 'Runner.%s'", | ||
event_loop_func); | ||
goto fail; | ||
} | ||
for (i = 0; i < nxt_nitems(handlers); i++) { | ||
obj = PyObject_GetAttrString(loop, handlers[i].key); | ||
if (nxt_slow_path(obj == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to get 'loop.%s'", | ||
handlers[i].key); | ||
goto fail; | ||
} | ||
|
||
*handlers[i].handler = obj; | ||
|
||
if (nxt_slow_path(PyCallable_Check(obj) == 0)) { | ||
nxt_unit_alert(NULL, "'loop.%s' is not a callable object", | ||
handlers[i].key); | ||
goto fail; | ||
} | ||
} | ||
|
||
obj = PyObject_CallObject(ctx_data->loop_create_future, NULL); | ||
if (nxt_slow_path(obj == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to create Future "); | ||
nxt_python_print_exception(); | ||
goto fail; | ||
} | ||
|
||
ctx_data->quit_future = obj; | ||
|
||
obj = PyObject_GetAttrString(ctx_data->quit_future, "set_result"); | ||
if (nxt_slow_path(obj == NULL)) { | ||
nxt_unit_alert(NULL, "Python failed to get 'future.set_result'"); | ||
goto fail; | ||
} | ||
|
||
ctx_data->quit_future_set_result = obj; | ||
|
||
if (nxt_slow_path(PyCallable_Check(obj) == 0)) { | ||
nxt_unit_alert(NULL, "'future.set_result' is not a callable object"); | ||
goto fail; | ||
} | ||
|
||
Py_DECREF(loop); | ||
Py_DECREF(asyncio); | ||
|
||
*pdata = ctx_data; | ||
|
||
return NXT_UNIT_OK; | ||
|
||
fail: | ||
|
||
nxt_python_asgi_ctx_data_free(ctx_data); | ||
|
||
Py_XDECREF(loop); | ||
Py_XDECREF(asyncio); | ||
|
||
return NXT_UNIT_ERROR; | ||
} | ||
|
||
#else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function can also be compacted to a single one with small preprocessor in it.
8cb7e0b
to
5a37171
Compare
Fixes for python asgi